package com.restkeeper.utils.device;

import cn.hutool.json.JSONObject;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Objects;

public class DevicePropertyUtils {

    public static JSONObject getIccidJson(String content){
        JSONObject object=new JSONObject(content);
        if(Objects.nonNull(object.get("items"))){
            Object itemsO = object.get("items");
            JSONObject items=new JSONObject(itemsO);
            if (items.containsKey("iccid")){
                return new JSONObject(items.get("iccid"));
            }
        }
        return null;
    }

    public static String getIccid(String content){
        JSONObject iccidJson = getIccidJson(content);
        if (iccidJson!=null){
            return iccidJson.get("value").toString();
        }
        return null;
    }

    public static String doSign(String toSignString, String secret, String signMethod) throws Exception {
        SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), signMethod);
        Mac mac = Mac.getInstance(signMethod);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(toSignString.getBytes());
        return Hex.encodeHexString(rawHmac);
    }


}
